package gov.pbc.nn.kjgl.webService.view;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.transform.TransformerException;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.stereotype.Component;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.WebServiceMessageFactory;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceMessageExtractor;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.DefaultMessageContext;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.mime.MimeMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor;
import org.springframework.ws.support.DefaultStrategiesHelper;
import org.springframework.ws.support.MarshallingUtils;
import org.w3c.dom.Node;
import org.dom4j.Document;
import org.dom4j.io.DOMReader;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.bdf2.webservice.client.WebServiceClient;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.variant.Record;
import com.bstek.dorado.uploader.DownloadFile;
import com.bstek.dorado.uploader.UploadFile;
import com.bstek.dorado.uploader.annotation.FileProvider;
import com.bstek.dorado.uploader.annotation.FileResolver;
import com.bstek.dorado.view.output.JsonBuilder;
import com.bstek.uflo.designer.utils.EscapeUtils;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.webService.endpoint.taskRequest;
import gov.pbc.nn.kjgl.webService.endpoint.taskResponse;

@Component("taskServiceSend")
public class taskServiceSend extends AbstractHibernateDao {
	public final static String uri = "http://localhost:8080/hc_llw/dorado/webservice/getTaskWebService";
	public final static String userName = "admin";
	public final static String password = "b1e0817dc088604e7d0ecb65d3c28e61bfd28e0e";
	
	private taskResponse sendAndReceive(final taskRequest request){
		final Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
		marshaller.setClassesToBeBound(new Class[] { taskRequest.class, taskResponse.class });
		marshaller.setMtomEnabled(true);
		WebServiceTemplate mtomClient=new WebServiceTemplate(marshaller);
		mtomClient.setDefaultUri(uri);
		Wss4jSecurityInterceptor wss4jInterceptor = new Wss4jSecurityInterceptor();
		wss4jInterceptor.setSecurementActions("UsernameToken");
		wss4jInterceptor.setSecurementUsername(taskServiceSend.userName);
		wss4jInterceptor.setSecurementPassword(taskServiceSend.password);
		wss4jInterceptor.setSecurementPasswordType("PasswordDigest");
		mtomClient.setInterceptors(new ClientInterceptor[] {wss4jInterceptor});	
		return mtomClient.sendAndReceive(new WebServiceMessageCallback() {
			public void doWithMessage(WebServiceMessage requestMessage) throws IOException, TransformerException {
				MarshallingUtils.marshal(marshaller, request, requestMessage);
			}
		}, new WebServiceMessageExtractor<taskResponse>() {
			public taskResponse extractData(WebServiceMessage response) throws IOException {
				return (taskResponse) MarshallingUtils.unmarshal(marshaller, response);
			}
		});			
	}
	
	
}
